public class BinaryTree {
    //定义树的节点
    public class TreeNode{
        //节点带的值
        public char val;
        //左孩子
        public TreeNode left;
        //右孩子
        public TreeNode right;
        //实例化时只需要传递值，因为不知道节点左右孩子的地址
        public TreeNode(char val) {
            this.val = val;
        }
    }
    //使用穷举的方式创建一颗二叉树,返回树的根节点
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    //前序遍历二叉树
    //传递根节点
    public void preOrder(TreeNode root) {
        //节点为空直接返回
        if(root == null) {
            return;
        }
        //打印根节点
        System.out.print(root.val+" ");
        //遍历左子树
        preOrder(root.left);
        //遍历右子树
        preOrder(root.right);
    }
    //中序遍历二叉树
    public void inOrder(TreeNode root) {
        //节点为空直接返回
        if(root == null) {
            return;
        }
        //先遍历左子树
        inOrder(root.left);
        //打印节点值
        System.out.print(root.val+" ");
        //遍历右子树
        inOrder(root.right);
    }
    //后续遍历二叉树
    public void lastOrder(TreeNode root) {
        //节点为空直接返回
        if (root == null) {
            return;
        }
        //遍历左子树
        lastOrder(root.left);
        //遍历右子树
        lastOrder(root.right);
        //打印节点值
        System.out.print(root.val+" ");
    }
}
